package db

import (
	"adm-mysql/utils"
	"context"
	"github.com/jackc/pgx/v5"
	"github.com/jackc/pgx/v5/pgxpool"
)

type Store struct {
	conn *pgxpool.Pool
	*Queries
}

func NewStore() *Store {
	return &Store{
		conn:    utils.DB,
		Queries: New(utils.DB),
	}
}
func (store *Store) ExecTx(ctx context.Context, fn func(queries *Queries) error) error {
	tx, err := store.conn.BeginTx(ctx, pgx.TxOptions{})
	if err != nil {
		return err
	}
	q := New(tx)
	err = fn(q)
	if err != nil {
		if rbErr := tx.Rollback(ctx); rbErr != nil {
			return rbErr
		}
		return err
	}
	return tx.Commit(ctx)
}
